package com.fr.report.js;

import com.fr.base.FCloneable;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.parameter.Parameter;
import com.fr.web.Repository;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AjaxJavaScript extends AbstractJavaScript
  implements CallBackable
{
  private static final String XML_TAG = "AjaxAttr";
  private boolean async = true;
  private String initContent;
  private String url;
  private String dataType = "auto";
  private JavaScript callBack;

  public String createJS(Repository paramRepository)
  {
    Map localMap = paramRepository.checkoutTplContext();
    Parameter[] arrayOfParameter = getParameters();
    JSONObject localJSONObject = new JSONObject();
    for (int i = 0; i < arrayOfParameter.length; ++i)
      try
      {
        localJSONObject.put(arrayOfParameter[i].getName(), arrayOfParameter[i].getValue());
      }
      catch (JSONException localJSONException)
      {
        FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      }
    String str = TemplateUtils.readTemplate2String("/com/fr/report/js/ajaxScript.tpl", "GBK");
    localMap.put("initContent", this.initContent);
    localMap.put("url", this.url);
    localMap.put("async", new Boolean(this.async));
    localMap.put("data", localJSONObject.toString());
    localMap.put("dataType", this.dataType);
    localMap.put("complete", "function(res, status){" + ((this.callBack != null) ? this.callBack.createJS(paramRepository) : "") + "}");
    try
    {
      return TemplateUtils.render(TemplateUtils.render(str, localMap), localMap);
    }
    catch (Exception localException)
    {
    }
    return "";
  }

  public String getInitContent()
  {
    return this.initContent;
  }

  public void setInitContent(String paramString)
  {
    this.initContent = paramString;
  }

  public String getUrl()
  {
    return this.url;
  }

  public void setUrl(String paramString)
  {
    this.url = paramString;
  }

  public boolean isAsync()
  {
    return this.async;
  }

  public void setAsync(boolean paramBoolean)
  {
    this.async = paramBoolean;
  }

  public String getDataType()
  {
    return this.dataType;
  }

  public void setDataType(String paramString)
  {
    this.dataType = paramString;
  }

  public void setCallBack(JavaScript paramJavaScript)
  {
    this.callBack = paramJavaScript;
  }

  public JavaScript getCallBack()
  {
    return this.callBack;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("AjaxAttr"))
      {
        this.initContent = paramXMLableReader.getAttr("init");
        this.url = paramXMLableReader.getAttr("url");
        this.async = (paramXMLableReader.getAttr("async") == null);
        String str2 = paramXMLableReader.getAttr("data");
        if (StringUtils.isNotBlank(str2))
          readPara(str2);
        this.dataType = paramXMLableReader.getAttr("dataType");
        String str3 = paramXMLableReader.getAttr("complete");
        if (StringUtils.isNotEmpty(str3))
          setCallBack(new JavaScriptImpl(str3));
      }
      else if ("JavaScript".equals(str1))
      {
        setCallBack(ReportXMLUtils.readJavaScript(paramXMLableReader));
      }
    }
  }

  private void readPara(String paramString)
  {
    int i;
    Parameter[] arrayOfParameter = getParameters();
    ArrayList localArrayList = new ArrayList();
    if (arrayOfParameter.length > 0)
      for (i = 0; i < arrayOfParameter.length; ++i)
        localArrayList.add(arrayOfParameter[i]);
    try
    {
      JSONObject localJSONObject = new JSONObject(paramString);
      Iterator localIterator = localJSONObject.keys();
      while (localIterator.hasNext())
      {
        Object localObject = localIterator.next();
        localArrayList.add(new Parameter(localObject.toString(), localJSONObject.opt(localObject.toString())));
      }
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    setParameters((Parameter[])localArrayList.toArray(new Parameter[0]));
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("AjaxAttr");
    if (StringUtils.isNotBlank(this.initContent))
      paramXMLPrintWriter.attr("init", this.initContent);
    if (StringUtils.isNotBlank(this.url))
      paramXMLPrintWriter.attr("url", this.url);
    if (!(this.async))
      paramXMLPrintWriter.attr("async", this.async);
    if (StringUtils.isNotBlank(this.dataType))
      paramXMLPrintWriter.attr("dataType", this.dataType);
    if (getCallBack() != null)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, getCallBack(), "JavaScript");
    paramXMLPrintWriter.end();
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof AjaxJavaScript) && (super.equals(paramObject)) && (((AjaxJavaScript)paramObject).async == this.async) && (ComparatorUtils.equals(((AjaxJavaScript)paramObject).getCallBack(), getCallBack())) && (ComparatorUtils.equals(((AjaxJavaScript)paramObject).dataType, this.dataType)) && (ComparatorUtils.equals(((AjaxJavaScript)paramObject).initContent, this.initContent)) && (ComparatorUtils.equals(((AjaxJavaScript)paramObject).url, this.url)));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    AjaxJavaScript localAjaxJavaScript = (AjaxJavaScript)super.clone();
    if (this.callBack != null)
      localAjaxJavaScript.callBack = ((JavaScript)this.callBack.clone());
    return localAjaxJavaScript;
  }
}